# 输入一个英文句子,翻转句子中的单词顺序,但单词内字符的顺序不变
from string import ascii_letters,digits,punctuation

sentence = 'No matter how bad your heart has been broken, the world doesn’t stop for your grief.'


#  方法1 用切片和拼接的方法生成

def reverse_sentence1(sentence: str):
    sentence = sentence.lower()
    last = sentence[-1]
    senlist = sentence[:-2].split()
    length = len(senlist)
    new_str = ''

    for i in range(-1, -length - 1, -1):
        new_str = new_str + senlist[i] + ' '
    new_str = new_str.capitalize().rstrip() + last

    return new_str


x = reverse_sentence1(sentence)

print(x)


# 方法2 用list反转和join的方式生成

def reverse_sentence2(sentence: str):
    sentence = sentence.lower()
    last = sentence[-1]
    senlist = sentence[:-2].split()
    senlist.reverse()
    new_str = " ".join(senlist).capitalize() + last

    return new_str


x = reverse_sentence2(sentence)

print(x)


# 最后的符号切割有问题，会导致得不到想要的结果。你取sentence[:-2]会漏掉最后一个字符，因为sentence[-2]这个索引值会漏掉。
# 两个方法都是这个问题
# 方法3


